package v1

import (
	"adminHome/models"
	"adminHome/pkg/app"
	"adminHome/pkg/e"
	"adminHome/pkg/setting"
	"adminHome/pkg/util"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"net/http"
)

//获取仓库列表
func GetStorage(c *gin.Context) {
	appG := app.Gin{c}
	name := c.Query("name")

	maps := make(map[string]interface{})
	data := make(map[string]interface{})

	if name != "" {
		maps["name"] = name
	}

	var status int = 1
	if arg := c.Query("status"); arg != "" {
		status = com.StrTo(arg).MustInt()
		maps["status"] = status
	}

	code := e.SUCCESS

	data["list"] = models.GetStorage(util.GetPage(c), setting.AppSetting.PageSize, maps)
	data["total"] = models.GetStorageTotal(maps)

	appG.Response(http.StatusOK, code, data)

}

func GetStorageById(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.Query("id")).MustInt()

	valid := validation.Validation{}
	valid.Required(id, "id").Message("id不能为空")

	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if !models.ExistStorageById(id) {
		code = e.ERROR_NOT_EXIST_STORAGE
		appG.Response(http.StatusOK, code, nil)
		return
	}

	code = e.SUCCESS
	storage, _ := models.GetStorageById(id)
	appG.Response(http.StatusOK, code, storage)
}

//新增
func AddStorage(c *gin.Context) {
	appG := app.Gin{c}
	name := c.PostForm("name")
	address := c.PostForm("address")
	type_ := com.StrTo(c.DefaultPostForm("type_", "1")).MustInt()
	remark := c.PostForm("remark")

	valid := validation.Validation{}
	valid.Required(name, "name").Message("名称不能为空")
	valid.Required(address, "address").Message("地址不能为空")
	valid.Required(type_, "type_").Message("类型不能为空")

	code := e.INVALID_PARAMS

	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if models.ExistStorageByName(name) {
		code = e.ERROR_EXIST_STORAGE
		appG.Response(http.StatusOK, code, nil)
		return
	}

	code = e.SUCCESS
	models.AddStorage(name, address, type_, remark)
	appG.Response(http.StatusOK, code, nil)
}

//修改
func EditStorage(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.PostForm("id")).MustInt()
	name := c.PostForm("name")
	address := c.PostForm("address")
	type_ := com.StrTo(c.PostForm("type_")).MustInt()
	remark := c.PostForm("remark")

	valid := validation.Validation{}
	valid.Required(id, "id").Message("id不能为空")
	valid.Required(name, "name").Message("名称不能为空")
	valid.Required(address, "address").Message("地址不能为空")
	valid.Required(type_, "type_").Message("类型不能为空")

	code := e.INVALID_PARAMS

	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if !models.ExistStorageById(id) {
		code = e.ERROR_NOT_EXIST_STORAGE
		appG.Response(http.StatusOK, code, nil)
		return
	}

	code = e.SUCCESS
	data := make(map[string]interface{})
	data["name"] = name
	data["address"] = address
	data["type"] = type_
	data["remark"] = remark

	models.EditStorage(id, data)
	appG.Response(http.StatusOK, code, nil)

}

//删除
func DeleteStorage(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.PostForm("id")).MustInt()

	valid := validation.Validation{}
	valid.Required(id, "id").Message("id不能为空")
	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if !models.ExistStorageById(id) {
		code = e.ERROR_NOT_EXIST_STORAGE
		appG.Response(http.StatusOK, code, nil)
		return
	}
	code = e.SUCCESS
	models.DeleteStorageById(id)
	appG.Response(http.StatusOK, code, nil)
}
